package com.example.networkchangedemo;

import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.PopupWindow;

import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;

import com.example.networkchangedemo.model.VideoDetailModel;
import com.example.networkchangedemo.weight.FullVideoPopupWindow;
import com.example.networkchangedemo.weight.TipPopupWinDow;
import com.example.networkchangedemo.weight.TipTopPopUpWindow;

import java.util.ArrayList;
import java.util.List;

import static android.view.Gravity.RIGHT;

import cn.jzvd.JZMediaSystem;
import cn.jzvd.JzvdStd;

public class MainActivity extends AppCompatActivity {
    Button btnBottom,btnLeft, btnRight,btnTop;
    ConstraintLayout cl_root;
    PopupWindow mPopupWindow,mTopPopupWindow;
    List<VideoDetailModel> detailModelList;
    private String videoUrl = "http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4";
    private JzvdStd jzvdStd;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initData();
        initListener();
    }

    private void initData() {
        detailModelList = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            VideoDetailModel detailModel = new VideoDetailModel();
            detailModel.setTitle("S1 与 Vision+ S1 App 的连接S1");
            detailModel.setPoint("01:10");
            detailModelList.add(detailModel);
        }
    }

    private void initView() {
        btnBottom = findViewById(R.id.btn_bottom);
        btnLeft = findViewById(R.id.btn_left);
        btnRight = findViewById(R.id.btn_right);
        btnTop = findViewById(R.id.btn_top);
        cl_root = findViewById(R.id.cl_root);
        jzvdStd = findViewById(R.id.jv_player);
        jzvdStd.setUp(videoUrl,"播放视频",JzvdStd.STATE_NORMAL);
        jzvdStd.startVideo();
    }

    private void initListener() {
        btnLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //浮框在左边
                FullVideoPopupWindow fullVideoPopupWindow = new FullVideoPopupWindow(MainActivity.this, detailModelList, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
                fullVideoPopupWindow.showAtLocation(v, Gravity.START,0, 0);
            }
        });
        btnBottom.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPopupWindow = new TipPopupWinDow(MainActivity.this, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
                //浮框在按钮下面
                showPopUpWindow(v);
                setBackgroundAlpha(MainActivity.this, 0.6f);
            }
        });
        btnRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //浮框在右边
                FullVideoPopupWindow fullVideoPopupWindow = new FullVideoPopupWindow(MainActivity.this,detailModelList,ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
                fullVideoPopupWindow.showAtLocation(v, RIGHT,0, 0);
            }
        });
        btnTop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mTopPopupWindow = new TipTopPopUpWindow(MainActivity.this, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
                showTopPopUpWindow(v);
                setBackgroundAlpha(MainActivity.this, 0.6f);
            }
        });
    }


    public static void setBackgroundAlpha(AppCompatActivity activity, float bgAlpha) {
        WindowManager.LayoutParams lp = activity.getWindow()
                .getAttributes();
        lp.alpha = bgAlpha;
        (activity).getWindow().setAttributes(lp);
    }

    private void showPopUpWindow(View v) {
        mPopupWindow.showAsDropDown(v);
        mPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                setBackgroundAlpha(MainActivity.this, 1.0f);
            }
        });
    }

    private void showTopPopUpWindow(View v) {
        //浮框在按钮下面
        ((TipTopPopUpWindow) mTopPopupWindow).showUp(v);
        mTopPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                setBackgroundAlpha(MainActivity.this, 1.0f);
            }
        });
    }
}